package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.model.entity.ApartmentInfo;
import com.atguigu.lease.model.entity.ViewAppointment;
import com.atguigu.lease.model.enums.AppointmentStatus;
import com.atguigu.lease.web.admin.mapper.ApartmentInfoMapper;
import com.atguigu.lease.web.admin.mapper.ViewAppointmentMapper;
import com.atguigu.lease.web.admin.service.ViewAppointmentService;
import com.atguigu.lease.web.admin.vo.appointment.AppointmentQueryVo;
import com.atguigu.lease.web.admin.vo.appointment.AppointmentVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author liubo
 * @description 针对表【view_appointment(预约看房信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class ViewAppointmentServiceImpl extends ServiceImpl<ViewAppointmentMapper, ViewAppointment> implements ViewAppointmentService {

    @Autowired
    private ApartmentInfoMapper apartmentInfoMapper;

    @Override
    public boolean updateStatusById(Long id, AppointmentStatus status) {
        // 参数校验
        if (id == null || status == null) {
            return false;
        }

        // 查询预约记录是否存在
        ViewAppointment viewAppointment = this.getById(id);
        if (viewAppointment == null || viewAppointment.getIsDeleted() == 1) {
            return false;
        }

        // 构建更新对象
        ViewAppointment updateAppointment = new ViewAppointment();
        updateAppointment.setId(id);
        updateAppointment.setAppointmentStatus(status);

        // 执行更新操作
        return this.updateById(updateAppointment);
    }

    @Override
    public IPage<AppointmentVo> pageAppointmentVo(Page<AppointmentVo> pageParam, AppointmentQueryVo queryVo) {
        // 构建查询条件
        LambdaQueryWrapper<ViewAppointment> wrapper = new LambdaQueryWrapper<>();

        // 添加软删除条件，0表示未删除
        wrapper.eq(ViewAppointment::getIsDeleted, 0);

        if (queryVo != null) {
            // 根据用户姓名模糊查询
            if (StringUtils.hasText(queryVo.getName())) {
                wrapper.like(ViewAppointment::getName, queryVo.getName());
            }

            // 根据手机号码查询
            if (StringUtils.hasText(queryVo.getPhone())) {
                wrapper.eq(ViewAppointment::getPhone, queryVo.getPhone());
            }

            // 根据公寓ID查询
            if (queryVo.getApartmentId() != null) {
                wrapper.eq(ViewAppointment::getApartmentId, queryVo.getApartmentId());
            }
        }

        // 按预约时间降序排序
        wrapper.orderByDesc(ViewAppointment::getAppointmentTime);

        // 执行分页查询
        Page<ViewAppointment> appointmentPage = new Page<>(pageParam.getCurrent(), pageParam.getSize());
        Page<ViewAppointment> resultPage = this.page(appointmentPage, wrapper);

        // 转换为AppointmentVo对象
        IPage<AppointmentVo> appointmentVoIPage = new Page<>(
                resultPage.getCurrent(),
                resultPage.getSize(),
                resultPage.getTotal()
        );

        List<ViewAppointment> appointmentList = resultPage.getRecords();
        if (appointmentList.isEmpty()) {
            appointmentVoIPage.setRecords(new ArrayList<>());
            return appointmentVoIPage;
        }

        // 获取所有公寓ID
        List<Long> apartmentIds = appointmentList.stream()
                .map(ViewAppointment::getApartmentId)
                .distinct()
                .collect(Collectors.toList());

        // 查询公寓信息
        LambdaQueryWrapper<ApartmentInfo> apartmentWrapper = new LambdaQueryWrapper<>();
        apartmentWrapper.in(ApartmentInfo::getId, apartmentIds);
        apartmentWrapper.eq(ApartmentInfo::getIsDeleted, 0);
        List<ApartmentInfo> apartmentInfoList = apartmentInfoMapper.selectList(apartmentWrapper);

        // 将公寓信息转为Map，方便后续查找
        Map<Long, ApartmentInfo> apartmentInfoMap = apartmentInfoList.stream()
                .collect(Collectors.toMap(ApartmentInfo::getId, apartmentInfo -> apartmentInfo));

        // 组装AppointmentVo对象
        List<AppointmentVo> appointmentVoList = appointmentList.stream().map(appointment -> {
            AppointmentVo appointmentVo = new AppointmentVo();
            // 复制ViewAppointment的属性到AppointmentVo
            BeanUtils.copyProperties(appointment, appointmentVo);

            // 设置公寓信息
            if (appointment.getApartmentId() != null) {
                ApartmentInfo apartmentInfo = apartmentInfoMap.get(appointment.getApartmentId());
                appointmentVo.setApartmentInfo(apartmentInfo);
            }

            return appointmentVo;
        }).collect(Collectors.toList());

        appointmentVoIPage.setRecords(appointmentVoList);

        return appointmentVoIPage;
    }
}




